package jxc.servlet.storage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxc.data.hibernate.dao.GoodsDAO;
import jxc.data.hibernate.dao.InportDAO;
import jxc.data.hibernate.dao.OutportDAO;
import jxc.data.hibernate.dao.SalesDAO;
import jxc.data.hibernate.dao.SalesbackDAO;
import jxc.data.hibernate.pojo.Goods;
import jxc.data.hibernate.pojo.Storagecheck;

/**
 * Servlet implementation class Check
 */
@WebServlet("/storage/check")
public class Check extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Storagecheck> checks=new ArrayList<Storagecheck>();
		InportDAO inDao=new InportDAO();
		OutportDAO oDao=new OutportDAO();
		SalesDAO sDao=new SalesDAO();
		SalesbackDAO sbDao=new SalesbackDAO();
		String gid=null;
		for(Goods good:new GoodsDAO().getAll()){
			gid=good.getId();
			checks.add(new Storagecheck(good, 
					inDao.countGoods(gid)+sbDao.countGoods(gid)-sDao.countGoods(gid)-oDao.countGoods(gid)
					));
		}
		request.setAttribute("list", checks);
		request.getRequestDispatcher("/storage/check.jsp").forward(request, response);
	}

}
